#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "transfile_op.h"

namespace aot{ namespace data{


transfile_op_impl::transfile_op_impl(void)
{
}

transfile_op_impl::~transfile_op_impl(void)
{
}

bool 
transfile_op_impl::insert(const itransfile_info* p, int& new_id)
{
    if (!g_db_user) return false;
   
    std::string str_full_file_name = p->full_file_name->c_str();
    std::string str_short_file_name = p->short_file_name->c_str();
    std::string str_file_size = p->file_size->c_str();
    std::string str_recv_datetime = p->recv_datetime->c_str();
    std::string str_sender_user_id = p->sender_user_id->c_str();
    std::string str_md5 = p->md5->c_str();
   
    remove_sin_quot(str_full_file_name);
    remove_sin_quot(str_short_file_name);
    remove_sin_quot(str_file_size);
    remove_sin_quot(str_recv_datetime);
    remove_sin_quot(str_sender_user_id);
    remove_sin_quot(str_md5);

    std::string sql_fmt = 
        "insert into file_trans \
        (full_file_name,\
        short_file_name,\
        file_size,\
        recv_datetime,\
        sender_user_id,\
        md5,\
        id,\
        send_or_recv,\
        file_type,\
        is_finish) \
        values\
        ('%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d);";

    std::string sql_exe;
    int t_new_id = get_table_next_id("file_trans");

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_full_file_name.c_str(),
        str_short_file_name.c_str(),
        str_file_size.c_str(),
        str_recv_datetime.c_str(),
        str_sender_user_id.c_str(),
        str_md5.c_str(),
        t_new_id,
        p->send_or_recv,
        p->file_type,
        p->is_finish

        );

    new_id = t_new_id;
    return g_db_user->exec(sql_exe.c_str());
}

bool 
transfile_op_impl::del(int nID)
{
    std::string sql_fmt = "delete from file_trans where id=%d;";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), nID);
    return g_db_user->exec(sql_exe.c_str());
}

bool 
transfile_op_impl::update(const itransfile_info* p)
{
    if (!g_db_user) return false;

    std::string str_full_file_name = p->full_file_name->c_str();
    std::string str_short_file_name = p->short_file_name->c_str();
    std::string str_file_size = p->file_size->c_str();
    std::string str_recv_datetime = p->recv_datetime->c_str();
    std::string str_sender_user_id = p->sender_user_id->c_str();
    std::string str_md5 = p->md5->c_str();

    remove_sin_quot(str_full_file_name);
    remove_sin_quot(str_short_file_name);
    remove_sin_quot(str_file_size);
    remove_sin_quot(str_recv_datetime);
    remove_sin_quot(str_sender_user_id);
    remove_sin_quot(str_md5);

    std::string sql_fmt = 

        "update file_trans set \
        full_file_name='%s',\
        short_file_name='%s',\
        file_size='%s',\
        recv_datetime='%s',\
        sender_user_id='%s',\
        md5='%s',\
        send_or_recv=%d, \
        file_type=%d,\
        is_finish=%d \
        where id=%d;";

    std::string sql_exe;

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_full_file_name.c_str(),
        str_short_file_name.c_str(),
        str_file_size.c_str(),
        str_recv_datetime.c_str(),
        str_sender_user_id.c_str(),
        str_md5.c_str(),
        p->send_or_recv,
        p->file_type,
        p->is_finish,
        p->id 
        );

    return g_db_user->exec(sql_exe.c_str());
}


bool 
transfile_op_impl::read(int id, isqlite3_table** out)
{
   std::string sql_fmt = "select \
                            id,\
                            full_file_name,\
                            short_file_name,\
                            file_size,\
                            recv_datetime,\
                            sender_user_id,\
                            md5,\
                            send_or_recv, \
                            file_type,\
                            is_finish \
                            from file_trans \
                            where id=%d;";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool 
transfile_op_impl::read(int id, itransfile_info* out)
{
    aot::data::table_helper t_table;
    bool ret =false;
    if ( this->read(id, t_table) )
    {
        if ( t_table.get_rows_count() > 0 )
        {
            t_table.set_row_index(0);

            out->id = t_table.get_field_value_int("id");
            out->file_type = t_table.get_field_value_int("file_type");
            out->send_or_recv = t_table.get_field_value_int("send_or_recv");
            out->is_finish = t_table.get_field_value_int("is_finish");

            out->full_file_name->assign(t_table.get_field_value_str("full_file_name").c_str());
            out->short_file_name->assign(t_table.get_field_value_str("short_file_name").c_str());
            out->file_size->assign(t_table.get_field_value_str("file_size").c_str());
            out->recv_datetime->assign(t_table.get_field_value_str("recv_datetime").c_str());
            out->sender_user_id->assign(t_table.get_field_value_str("sender_user_id").c_str());
            out->md5->assign(t_table.get_field_value_str("md5").c_str());

            ret = true;
        }
    }
    return ret;
}

bool 
transfile_op_impl::read(int id, itransfile_info** out)
{
    aot::data::itransfile_info* obj = new aot::data::transfile_info_impl();
    *out = obj;
    return this->read(id, obj);
}

bool 
transfile_op_impl::get_all_info(isqlite3_table** out)
{
    std::string sql_fmt = "select \
                            id,\
                            full_file_name,\
                            short_file_name,\
                            file_size,\
                            recv_datetime,\
                            sender_user_id,\
                            md5,\
                            send_or_recv,\
                            file_type,\
                            is_finish \
                            from file_trans;";

    std::string sql_exe = sql_fmt;
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool 
transfile_op_impl::get_info_by_user_id(const char* user_id, isqlite3_table** out)
{
    std::string sql_fmt = "select \
                          id,\
                          full_file_name,\
                          short_file_name,\
                          file_size,\
                          recv_datetime,\
                          sender_user_id,\
                          md5,\
                          send_or_recv,\
                          file_type,\
                          is_finish \
                          from file_trans \
                          where sender_user_id='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), user_id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool 
transfile_op_impl::get_info_by_user_id(const char* user_id, const char* fn, const char* md5, isqlite3_table** out)
{
    std::string sql_fmt = "select \
                          id,\
                          full_file_name,\
                          short_file_name,\
                          file_size,\
                          recv_datetime,\
                          sender_user_id,\
                          md5,\
                          send_or_recv,\
                          file_type,\
                          is_finish \
                          from file_trans \
                          where sender_user_id='%s' and short_file_name='%s' and md5='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), user_id, fn, md5);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

}} /* end namespace aot/data*/